package cn.enjoy.rabbitmq.handler.boot;

import cn.enjoy.rabbitmq.config.MqQueueConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 工作队列
 * 消费者竞争消费，一条消息只会传递给一个消费者
 *
 * @author K
 * @date 2020/11/27 17:33
 */
@Slf4j
@Component
@RabbitListener(queues = MqQueueConfig.QUEUE_WORK)
public class WorkQueueListener2 {

    @RabbitHandler
    public void fanoutHandlerManualAck(Map msg, Message message, Channel channel) {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("work queue 工作队列模式，Listener2 接收到的消息为：{}", msg.toString());
            channel.basicAck(deliveryTag, false);
        } catch (Exception e) {
            try {
                channel.basicNack(deliveryTag, false, false);
            } catch (Exception e1) {
                e1.printStackTrace();
            }
        }
    }

}



